import pygame.sprite
#!/usr/bin/env python

try:
	import pdb
	import pygame
	import sys
	from data.scenarios import *
	from sprite.battlefield import *
	from sprite.infobar import *
	#~ from command import Command
	#~ from sprite.creature import Creature
	from sprite.cursor import *
	from sprite.panel import *
	from sprite.units import *
	from pygame.locals import *
	#~ from utils import *
except ImportError, err:
	print "Could not load %s module." % (err)
	raise SystemExit


class ScenarioEngine(object):
	def __init__(self):
		object.__init__(self)

		#~ self.clock = clock
		self.screen = pygame.display.get_surface()
		#~ self.scen_data = ScenarioDef()
		self.animating = False

		self.updaterects = None

		# init layers (map, creatures, other, cursor)
		self.layers = pygame.sprite.LayeredDirty()

		self.field = Battlefield(scenarios[1].id, self.layers)
		self.info_bar = InfoBar(self.field, self.layers)
		self.units = Units(self.field, self.layers)

		self.layers.add(self.field)
		self.layers.add(self.info_bar, layer = 3)
		self.layers.add(self.units, layer = 2)

#		self.layers = []
#		self.layers.append(pygame.sprite.RenderPlain(self.field))
#		self.layers.append(pygame.sprite.RenderPlain(self.info_bar))
#		self.layers.append(pygame.sprite.RenderPlain())
#		self.layers.append(pygame.sprite.RenderPlain())


		cursor = Cursor(self.layers)

		self.inputstack = []
		self.inputstack.append(cursor)

		#sets cursor for the info bar and updates it
		self.info_bar.set_cursor(cursor)

		#Set a blank background for the layers
		background = pygame.Surface(pygame.display.get_surface().get_size())
		self.layers.clear(None, background)

		#~ self.command = None
		#~ self.window = None list
		#self.sys_menu = None

	def draw_layers(self):
		#self.screen.fill((0,0,0)) # to fill the screen if nothing drawn
		self.layers.update()
		self.updaterects = self.layers.draw(self.screen)
		#[[l.update(), l.draw(self.screen)] for l in self.layers]

	def do_cancel(self):
		self.inputstack[-1].close()
		return

	def finish_window_close(self):
		"""
		Finishes closing the window. Called on USEREVENT - WINDOW_CLOSED
		"""
		# called on USEREVENT WINDOW_CLOSED

		#temp bug fix - hitting enter or backspace too fast would cause residual
		#note - fix leaves a black residual on the unit overlay layer on a command
		self.inputstack.pop().kill()

		self.inputstack[-1].show()
		return

	def do_cmove(self, key, mod):
		try:
			self.inputstack[-1].c_move(key, mod) or [spr.r_move(key, mod) for spr in self.layers.get_sprites_from_layer(0)]
		except:
			pass

	def do_move(self, key, mod):
		try:
			self.inputstack[-1].move(key, mod)
		except:
			pass

#		(
#			[spr.r_move(key, mod) for spr in self.layers[0]] and
#			[spr.r_move(key, mod) for spr in self.layers[1]]
#		)

	def do_action(self):
		self.inputstack[-1].do_action(self)
		return

	def load_sys_menu(self):
		self.inputstack.append(SystemMenu(self.layers))
		self.inputstack[-1].open()
#		if self.sys_menu is None:
#			self.sys_menu = SystemMenu(self.layers)
#		self.sys_menu.open()

	def load_creature_menu(self, creature):
		self.inputstack.append(CreatureMenu(self.layers, creature))
		self.inputstack[-1].open()


	def is_animating(self):
		if self.inputstack[-1].animating == [] or [[],[]]:
			return False
		return self.inputstack[-1].animating
